<template>
  <div class="testpaper-cjian" v-if="isShow">
    <el-card class="box-card">
      <!-- 按钮 -->
      <div class="filter-bar">
        <div class="lt">
          <b>试卷</b>
        </div>
        <el-button class="btn" size="small" type="primary" plain @click="houtui"
          >返回列表</el-button
        >
      </div>

      <!-- 表单 -->
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-row type="flex">
          <el-col :span="13">
            <el-form-item label="试卷名称" prop="paper_name">
              <el-input v-model="ruleForm.paper_name" style="width: 700px" />
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item label="试卷性质" prop="check_state">
              <el-checkbox-group
                v-model="ruleForm.check_state"
                @change="change"
              >
                <el-checkbox ref="check1" label="1" name="type" :checked="false"
                  >单选题</el-checkbox
                >
                <el-checkbox ref="check2" label="2" :checked="false" name="type"
                  >多选题</el-checkbox
                >
                <el-checkbox ref="check3" label="3" :checked="false" name="type"
                  >判断题</el-checkbox
                >
                <el-checkbox ref="check4" label="4" :checked="false" name="type"
                  >简答题</el-checkbox
                >
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="16">
            <el-form-item label="归属学科" prop="subject_id">
              <el-select
                v-model="ruleForm.subject_id"
                placeholder="请选择活动区域"
              >
                <el-option
                  v-for="item in treeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="16">
            <el-form-item label="归属目录" prop="directory_id">
              <el-select
                v-model="ruleForm.directory_id"
                placeholder="请选择活动区域"
              >
                <el-option
                  v-for="item in children1"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="试卷状态">
              <el-radio-group v-model="ruleForm.show_state">
                <el-radio label="1">启用</el-radio>
                <el-radio label="2">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col>
            <el-form-item label="试卷类型">
              <el-radio-group v-model="ruleForm.paper_type">
                <el-radio label="1">模拟卷</el-radio>
                <el-radio label="2">考试卷</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="6">
            <el-form-item label="试卷总分" prop="total_score">
              <el-input v-model="ruleForm.total_score" style="width: 150px">
                <template slot="append">分</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="及格分数" prop="pass_score">
              <el-input v-model="ruleForm.pass_score" style="width: 150px">
                <template slot="append">分</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="试卷难度" prop="difficulty">
              <el-select
                v-model="ruleForm.difficulty"
                placeholder="请选择活动区域"
              >
                <el-option label="简单" value="1" />
                <el-option label="中等" value="2" />
                <el-option label="较难" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="选题信息">
          <el-table border :data="tableData" style="width: 100%">
            <el-table-column label="题型" width="140" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.check_question_list.name }}</span>
              </template>
            </el-table-column>
            <el-table-column label="选题信息" min-width="800">
              <template slot-scope="scope">
                <div>
                  共<el-input
                    v-model="xyaoquantity"
                    class="listInput"
                    placeholder=""
                    @blur="addquantity(scope.row)"
                  />个,每题<el-input
                    v-model="fraction"
                    class="listInput"
                    placeholder=""
                  />
                  <span
                    >分, 已选题数：<i style="color: #409eff">{{ quantity }}</i>
                    题
                  </span>
                  <el-button type="success" size="small" @click="kanClick"
                    >看题</el-button
                  >
                  <el-button
                    type="primary"
                    size="small"
                    @click="xtClick(scope.row)"
                    >选题</el-button
                  >
                </div>
              </template>
            </el-table-column>
            <template slot="empty">
              <P> 请勾选试卷包含题型，从而生成选题模板</P>
            </template>
          </el-table>
        </el-form-item>
        <el-row class="establish">
          <el-col>
            <el-form-item>
              <el-button type="primary" @click.native="establish"
                >立即创建</el-button
              >
              <el-button>取消</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 选题弹出层 -->
    <el-dialog title="选择试题 " :visible.sync="xtPopup" width="70%">
      <div class="subject">
        <el-form
          ref="formData1"
          :inline="true"
          :model="formInline"
          class="demo-form-inline"
        >
          <el-row type="flex" class="row-bg" justify="space-around">
            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="formInline.subject_id"
                  placeholder="请选择学科"
                  size="small"
                >
                  <el-option
                    v-for="item in treeList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="formInline.directory_id"
                  placeholder="请选择目录"
                  size="small"
                >
                  <el-option
                    v-for="item in children"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="formInline.tag_id"
                  placeholder="请选择标签"
                  size="small"
                >
                  <el-option
                    v-for="item in tag"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="check"
                  disabled
                  placeholder="请选择"
                  size="small"
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex" class="row-bg" justify="space-around">
            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="formInline.quest_type"
                  disabled
                  placeholder="审核通过"
                  size="small"
                >
                  <el-option
                    v-for="item in shelf"
                    :key="item.value"
                    :value="item.value"
                    :label="item.label"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="formInline.show_state"
                  disabled
                  placeholder="上架"
                  size="small"
                >
                  <el-option
                    v-for="item in Put"
                    :key="item.value"
                    :value="item.value"
                    :label="item.label"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item>
                <el-input
                  v-model="formInline.create_name"
                  placeholder="请输入创建者"
                  size="small"
                />
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="formInline.difficulty"
                  placeholder="请选择难度"
                  size="small"
                >
                  <el-option
                    v-for="item in diff"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex" class="row-bg" justify="space-around">
            <el-col :span="6">
              <el-form-item>
                <el-select
                  v-model="formInline.is_true_question"
                  placeholder="请选择是否真题"
                  size="small"
                  style="width: 200px"
                >
                  <el-option value="1" label="是">是</el-option>
                  <el-option value="2" label="否">否</el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col>
              <el-form-item class="el-col el-col-5">
                <el-date-picker
                  v-model="time"
                  class="datetimerange"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  size="small"
                />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item class="el-col el-col-13 el-col-offset-10">
                <el-button size="mini" type="primary" @click="onSubmit"
                  >查询</el-button
                >
                <el-button size="mini" @click.native="resetForm"
                  >重置</el-button
                >
                <el-button
                  size="mini"
                  type="warning"
                  plain
                  :icon="
                    detailed ? 'el-icon-folder-add' : 'el-icon-folder-remove'
                  "
                  @click="detailed = !detailed"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="Truetopic"
          tooltip-effect="dark"
          style="width: 100%"
          border
        >
          <el-table-column type="selection" width="55" />
          <el-table-column label="试题信息">
            <template slot-scope="{ row }">
              <div>
                <div>
                  <span class="ltColor">ID: {{ row.id }}</span
                  ><span class="information ltColor">题干信息: </span>
                  <el-link type="primary"> <i v-html="row.question" /></el-link>
                </div>

                <!-- 详细信息 -->
                <div v-show="detailed">
                  <el-row type="flex" class="row-bg" justify="space-around">
                    <el-col :span="13">
                      <span class="ltColor">试题编号: </span
                      ><i>{{ row.quest_num }}</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">试题类型: </span>
                      <i v-if="row.quest_type === 1">单选题</i>
                      <i v-if="row.quest_type === 2">多选题</i>
                      <i v-if="row.quest_type === 3">判断题</i>
                      <i v-if="row.quest_type === 4">简答题</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">归属学科: </span>
                      <i>{{ row.subject_name }}</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">归属目录: </span>
                      <i>{{ row.directory_name }}</i>
                    </el-col>
                  </el-row>

                  <el-row type="flex" class="row-bg" justify="space-around">
                    <el-col :span="13">
                      <span class="ltColor">标签信息: </span>
                      <i>{{ row.tag_info[0] }}</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">发布状态: </span>
                      <i v-if="row.show_state === 1"
                        ><el-tag type="success">上架</el-tag></i
                      >
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">审核人: </span>
                      <i>暂无</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">审核状态: </span>
                      <i v-if="row.quest_type === 2"
                        ><el-tag type="success">通过</el-tag></i
                      >
                    </el-col>
                  </el-row>

                  <el-row type="flex" class="row-bg" justify="space-around">
                    <el-col :span="13">
                      <span class="ltColor">审核意见: </span>
                      <i>{{ row.check_remarks }}</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">创建人: </span>
                      <i>{{ row.create_name }}</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">难度: </span>
                      <i v-if="row.difficulty === 1">简单</i>
                      <i v-if="row.difficulty === 2">中等</i>
                      <i v-if="row.difficulty === 3">较难</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">发布时间: </span>
                      <i>{{ row.created_at | formaDate("YYYY-MM-DD") }}</i>
                    </el-col>
                  </el-row>
                </div>

                <!-- 模糊信息 -->
                <div v-show="!detailed">
                  <el-row type="flex" class="row-bg" justify="space-around">
                    <el-col :span="6">
                      <span class="ltColor">归属学科: </span>
                      <i>{{ row.subject_name }}</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">归属目录: </span>
                      <i>{{ row.directory_name }}</i>
                    </el-col>

                    <el-col :span="13">
                      <span class="ltColor">标签信息: </span>
                      <i>{{ row.tag_info[0] }}</i>
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">发布状态: </span>
                      <i v-if="row.show_state === 1"
                        ><el-tag type="success">上架</el-tag></i
                      >
                    </el-col>

                    <el-col :span="6">
                      <span class="ltColor">发布时间: </span>
                      <i>{{ row.created_at | formaDate("YYYY-MM-DD") }}</i>
                    </el-col>
                  </el-row>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="xtPopup = false">取 消</el-button>
        <el-button type="primary" @click="Popupbtn">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查看选择的题 弹出层 -->
    <el-dialog title="查阅试题" :visible.sync="choice" width="70%">
      <el-table :data="questionsData" style="width: 100%" border>
        <el-table-column label="试题内容">
          <template slot-scope="{ row }">
            <div>
              <div>
                <span class="ltColor">ID: {{ row.id }}</span
                ><span class="information ltColor">题干信息: </span>
                <el-link type="primary"> <i v-html="row.question" /></el-link>
              </div>
              <el-row type="flex" class="row-bg" justify="space-around">
                <el-col :span="6">
                  <span class="ltColor">归属学科: </span>
                  <i>{{ row.subject_name }}</i>
                </el-col>

                <el-col :span="6">
                  <span class="ltColor">归属目录: </span>
                  <i>{{ row.directory_name }}</i>
                </el-col>

                <el-col :span="13">
                  <span class="ltColor">标签信息: </span>
                  <i>{{ row.tag_info[0] }}</i>
                </el-col>

                <el-col :span="6">
                  <span class="ltColor">发布状态: </span>
                  <i v-if="row.show_state === 1"
                    ><el-tag type="success">上架</el-tag></i
                  >
                </el-col>

                <el-col :span="6">
                  <span class="ltColor">发布时间: </span>
                  <i>{{ row.created_at | formaDate("YYYY-MM-DD") }}</i>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="centne">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { xtClick, edit, editData, establish } from "@/api/exam";
import { mapGetters, mapActions } from "vuex";
export default {
  name: "TestpaperCjian",
  components: {},
  filters: {
    // 设置一个函数来进行过滤
    formaDate: function (dateStr, pattern = "") {
      const str = parseInt(dateStr.toString().padEnd(13, "0"));
      const dt = new Date(str);
      const yy = dt.getFullYear();
      // ES2017 引入了字符串补全长度的功能。如果某个字符串不够指定长度，会在头部或尾部补全。padStart()用于头部补全，padEnd()用于尾部补全。
      const mm = (dt.getMonth() + 1).toString().padStart(2, "0");
      const dd = dt.getDate().toString().padStart(2, "0");
      const h = dt.getHours().toString().padStart(2, "0");
      const m = dt.getMinutes().toString().padStart(2, "0");
      const s = dt.getSeconds().toString().padStart(2, "0");
      if (pattern.toLowerCase() === "yyyy-mm-dd") {
        return yy + "-" + mm + "-" + dd;
      } else {
        return yy + "-" + mm + "-" + dd + " " + h + ":" + m + ":" + s;
      }
    },
  },
  props: {
    list: {
      type: Array,
    },
  },
  data() {
    return {
      isShow: true,
      ruleForm: {
        page: 1,
        limit: 10,
        paper_name: "",
        check_state: [],
        directory_id: "",
        paper_type: "1",
        show_state: "1",
        subject_id: "",
        total_score: 100,
        pass_score: 60,
        quest_type: "",
      },
      rules: {
        paper_name: [
          { required: true, message: "请输入试卷名称", trigger: "blur" },
        ],
        check_question_list: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个",
            trigger: "change",
          },
        ],
        directory_id: [{ required: true }],
        subject_id: [{ required: true }],
        total_score: [{ required: true }],
        pass_score: [{ required: true }],
        quest_type: [{ required: true }],
      },
      quest: {
        page: 1,
        limit: 10,
        subject_id: "",
        directory_id: "",
        tag_id: "",
        difficulty: "",
        quest_type: null,
        is_true_question: "",
        show_state: 1,
        check_state: 2,
        create_name: "",
        create_time: "",
      },
      tableData: [
        // {
        //   check_question_list: {
        //     check_list: [],
        //     name: '多选题',
        //     num: 5,
        //     score: 20,
        //     type: 2
        //   }
        // }
      ],
      ind: [
        {
          check_question_list: {
            check_list: [],
            name: "单选题",
            num: 0,
            score: 0,
            type: 1,
          },
        },
        {
          check_question_list: {
            check_list: [],
            name: "多选题",
            num: 0,
            score: 0,
            type: 2,
          },
        },
        {
          check_question_list: {
            check_list: [],
            name: "判断题",
            num: 0,
            score: 0,
            type: 3,
          },
        },
        {
          check_question_list: {
            check_list: [],
            name: "简答题",
            num: 0,
            score: 0,
            type: 4,
          },
        },
      ],

      xtPopup: false, // 选题弹出层
      // 选题弹出层表单域
      formInline: {
        page: 1,
        limit: 10,
        quest_type: 2, // 审核
        show_state: 1, // 上架
        check_state: 2, // 单选题 多选题
        subject_id: "", // 学科
        directory_id: "", // 目录
        tag_id: "", // 标签
        create_name: "", // 创建者
        is_true_question: "", // 是否为真题
        create_time: "", // 时间
        difficulty: "", // 难度
      },
      time: [],
      // 多选题禁用框
      options: [
        {
          value: "1",
          label: "单选题",
        },
        {
          value: "2",
          label: "多选题",
        },
        {
          value: "3",
          label: "判断题",
        },
        {
          value: "4",
          label: "简答题",
        },
      ],

      // 审核通过
      shelf: [{ value: 2, label: "审核通过" }],
      // 上架
      Put: [{ value: 1, label: "上架" }],
      // 试卷难度
      diff: [
        {
          value: "1",
          label: "简单",
        },
        {
          value: "2",
          label: "中等",
        },
        {
          value: "3",
          label: "较难",
        },
      ],

      // 试题数据
      Truetopic: [],
      // 试题信息详情
      detailed: false,
      // 选中的试题信息
      questionsData: [],
      // 需要的题数
      xyaoquantity: 0,
      // 每题分数
      fraction: 0,

      // 查看题 弹出层
      choice: false,

      // 题型
      diffrd: [],

      // created_at
      created_at: 0,
      //  creator_id
      creator_id: 0,
      // create_name
      create_name: "",
      // directory_name
      directory_name: "",
      // quest_type
      // quest_type: [],
      // question_list
      question_list: [],
      // subject_name
      subject_name: "",
      // quest_type
      // quest_type: []
      pid: 0,
    };
  },
  computed: {
    ...mapGetters({
      treeList: "subject/treeList",
    }),
    directoryList() {
      // 目录信息
      if (!this.query.subject_id) {
        return [];
      } else {
        // eslint-disable-next-line vue/no-side-effects-in-computed-properties
        this.query.directory_id = "";
        const result = this.treeList.find(
          (val) => val.id === this.query.subject_id
        );
        return result.children;
      }
    },
    createTime() {
      // 创建时间
      if (this.time.length) {
        return this.time.map((val) => val).join(",");
      } else {
        return "";
      }
    },

    quest_type() {
      return this.ruleForm.check_state[this.ruleForm.check_state.length - 1];
    },

    children() {
      if (this.formInline.subject_id || this.ruleForm.subject_id) {
        const result = this.treeList.find(
          (val) =>
            val.id === this.formInline.subject_id || this.ruleForm.subject_id
        );
        return result.children;
      } else {
        return [];
      }
    },

    children1() {
      if (this.ruleForm.subject_id) {
        const result = this.treeList.find(
          (val) => val.id === this.ruleForm.subject_id
        );
        return result.children;
      } else {
        return [];
      }
    },

    tag() {
      if (this.formInline.directory_id) {
        return this.children.find(
          (val) => val.id === this.formInline.directory_id
        ).children;
      } else {
        return [];
      }
    },

    check() {
      return this.options[this.formInline.check_state - 1].label;
    },

    culty() {
      return this.diff[this.formInline.difficulty];
    },

    // 选中的题数
    quantity() {
      return this.questionsData.length;
    },
    // 分数总数
    fractionNum() {
      if (this.quantity * this.quantity === 100) {
        return 1;
      } else {
        return 0;
      }
    },
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    houtui() {
      window.location.reload();
    },
    // 初始化
    async init(id) {
      this.pid = id;
      if (id) {
        const data = await edit(id);
        const quest_type = data.quest_type.substring(
          1,
          data.quest_type.length - 1
        );
        // console.log(data.check_question_list.substring(1, data.check_question_list.length - 1).replace('},', '}|').split('|'))
        const add = data.check_question_list
          .substring(1, data.check_question_list.length - 1)
          .replace("},", "}|")
          .split("|");
        add.forEach((item) => {
          this.diffrd.push({ check_question_list: JSON.parse(item) });
        });
        this.ruleForm.paper_name = data.paper_name;
        this.ruleForm.check_state = quest_type.split(",");
        this.ruleForm.subject_id = data.subject_id;
        this.ruleForm.directory_id = data.directory_id;
        this.ruleForm.show_state = data.show_state.toString();
        this.ruleForm.paper_type = data.paper_type.toString();
        this.ruleForm.total_score = data.total_score;
        this.ruleForm.pass_score = data.pass_score;
        this.ruleForm.difficulty = data.difficulty.toString();
        this.tableData = this.diffrd;
        this.questionsData = data.question_data;
        this.xyaoquantity = this.diffrd[0].check_question_list.num;
        this.fraction = this.diffrd[0].check_question_list.score;

        this.created_at = data.created_at;
        this.creator_id = data.creator_id;
        this.create_name = data.create_name;
        this.question_list = data.question_list;
        this.subject_name = data.subject_name;
        console.log(data);
      } else {
        return;
      }
    },

    async xtClick(row) {
      console.log(row);
      this.quest.quest_type = row.check_question_list.type;
      const res = await xtClick(this.quest);
      this.Truetopic = res.list;
      console.log(res.list);
      this.xtPopup = true;
    },
    change(val) {
      this.tableData = [];
      var arr = [];
      this.ind.forEach((item) => {
        arr.push(item);
      });
      var resultArr = val.map((i) => arr[i - 1]);
      resultArr.forEach((data) => {
        this.tableData.push(data);
      });
    },

    // 查询
    async onSubmit() {
      const res = await xtClick(this.formInline);
      console.log(res);
      this.Truetopic = res.list;
    },

    // 试题添加
    Popupbtn() {
      this.questionsData = this.$refs.multipleTable.selection;
      this.xtPopup = false;
      this.$message.warning("添加成功");
    },

    // 重置
    resetForm() {
      this.formInline = {
        page: 1,
        limit: 10,
        quest_type: 2, // 审核
        show_state: 1, // 上架
        check_state: 2, // 单选题 多选题
        subject_id: "", // 学科
        directory_id: "", // 目录
        tag_id: "", // 标签
        create_name: "", // 创建者
        is_true_question: "", // 是否为真题
        create_time: "", // 时间
        difficulty: "", // 难度
      };
      this.onSubmit();
    },

    // 每题数
    addquantity(data) {
      // console.log(data.check_question_list)
      // xyaoquantity
      data.check_question_list.num = this.xyaoquantity;
      // this.ruleForm.quest_type = this.tableData
      this.tableData[0].check_question_list.num = this.xyaoquantity;
      if (!this.xyaoquantity * 1 === this.quantity * 1) {
        return this.$message.warning("题数不一致");
      }
      if (this.fractionNum === 1) {
        return this.$message.warning("分数不一致");
      }
    },

    // 创建
    establish() {
      this.$refs.ruleForm.validate(async (val) => {
        if (val) {
          if (!this.pid === 0) {
            console.log(this.did, "aaa");

            const list = {
              check_question_list: this.tableData,
              create_name: this.create_name,
              created_at: this.created_at,
              creator_id: this.creator_id,
              difficulty: this.ruleForm.difficulty,
              directory_id: this.ruleForm.directory_id,
              directory_name: this.directory_name,
              id: this.did,
              paper_name: this.ruleForm.paper_name,
              paper_type: this.paper_typeOne,
              pass_score: this.ruleForm.pass_score,
              quest_type: this.check_state,
              questionsData: this.questionsData,
              question_list: this.question_list,
              show_state: this.ruleForm.show_state,
              subject_id: this.ruleForm.subject_id,
              subject_name: this.subject_name,
              total_score: this.ruleForm.total_score,
            };
            await editData(treeList);
          } else {
            console.log("bbb");
            // 选中的题目 ID
            const listid = [];
            this.questionsData.forEach((item) => {
              listid.push(item.id);
            });
            const typepass = [];
            this.ruleForm.check_state.forEach((item) => {
              typepass.push(item * 1);
            });
            // 获取题目详情
            const check_question_list = [];
            this.tableData.forEach((item) => {
              check_question_list.push(
                JSON.parse(JSON.stringify(item)).check_question_list
              );
            });
            const data = {
              check_question_list: check_question_list,
              difficulty: this.ruleForm.difficulty * 1,
              directory_id: this.ruleForm.directory_id,
              paper_name: this.ruleForm.paper_name,
              paper_type: this.ruleForm.paper_type * 1,
              pass_score: this.ruleForm.pass_score,
              quest_type: typepass,
              question_list: listid,
              show_state: this.ruleForm.show_state * 1,
              subject_id: this.ruleForm.subject_id,
              total_score: this.ruleForm.total_score,
            };
            await establish(data);
          }
          this.$emit("update");
        } else {
          return this.$message.warning("检查提交数据，校验失败");
        }
      });
    },

    // 查看
    kanClick() {
      if (this.questionsData.length) {
        this.choice = true;
      } else {
        this.$message.warning("还未选题，没有试题信息");
      }
    },
    // 删除查看题
    handleDelete(val) {
      const id = val.id;
      this.questionsData = this.questionsData.filter((item) => item.id !== id);
      this.$message.warning("删除成功");
    },

    // 返回
    fanhui() {
      this.$emit("update");
    },
  },
};
</script>
<style lang="scss" scoped>
b {
  display: block;
  padding-top: 5px;
}
.listInput {
  width: 60px;
}
.filter-bar {
  display: flex;
  justify-content: space-between;
  flex-direction: flex-end;
  padding-bottom: 5px;
  border-bottom: 1px solid #eaeaea;
}

.demo-ruleForm {
  margin-top: 15px;
}

::v-deep .el-dialog__header {
  border-bottom: 1px solid #e9ecef;
}

.row-bg {
  height: 40px;
}

.datetimerange {
  width: 260px;
  font-size: 16px;
  margin-left: 63px;
}

.information {
  margin-left: 30px;
}

.ltColor {
  color: #aaa;
}

.establish {
  padding-left: 100px;
}
</style>
